﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PServiceBus.Core.Runtime;

namespace PServiceBus.Tests.Unit {
    [TestClass]
    public class ExpressionGeneratorTest {
        
        [TestMethod]
        public void ExpressionWithTwoConditionShouldReturnTrue() {
            var expression = ExpressionGenerator.Build("test1    =a && test2 = a ");
            Assert.IsTrue(expression("a", "a"), "test1 = a should be true, test2 = a should be true");
        }

        [TestMethod]
        public void TestGuidComparison() {
            var guid = new Guid("54779b3a-870d-400b-bb89-10c4f91004be");
            var expression = ExpressionGenerator.Build("SessionID=54779b3a-870d-400b-bb89-10c4f91004be");
            var result = expression(guid.ToString());
            Assert.IsTrue(result);
        }

        [TestMethod]
        public void TestThatRepeatedConditionIsNotFalse() {
            var expressionText = "Test1 = \"19A03736-CAFB-40CE-A3CF-E64CF7AFB7D7\"";
            var expression = ExpressionGenerator.Build(expressionText);
            var result = expression("19A03736-CAFB-40CE-A3CF-E64CF7AFB7D7");
            //var test1Is2 = expression("Test1", "2");
            //Assert.IsTrue(count == 2, "Count should be 1");
            Assert.IsTrue(result, "Test1 should equal true");
            //Assert.IsTrue(test1Is2, "Test1 should equal 2");
        }

        [TestMethod]
        public void TestRegexConditionWorks() {
            var expressionText = "test1 regex \"\\b(yes)\\b\"";
            var expression = ExpressionGenerator.Build(expressionText);
            var result = expression("test yes world");
        }

        [TestMethod]
        public void TestStringWithConditionWorks() {
            var expressionText = "test1 = \"Hello(Test)\"";
            var expression = ExpressionGenerator.Build(expressionText);
            var result = expression("Hello(Test)");
        }

        [TestMethod]
        public void TestThatConditionIsNotTrueAndIsTrue() {
            var exp = "UserName != \"Olamide\" || UserName = \"TM\" || Message = \"Test\"";
            var expression = ExpressionGenerator.Build(exp);
            var isNotOlamideIsTrue = expression("Test", "TM");
            var isNotOlamideIsFalse = expression("Tester", "Olamide");
            Assert.IsTrue(isNotOlamideIsTrue);
            Assert.IsFalse(isNotOlamideIsFalse);
        }
    }
}
